/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : usb_device.c
  * @version        : v2.0_Cube
  * @brief          : This file implements the USB Device
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "tim.h"
#include "usb_device.h"
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_common.h"
#include "usbd_composite.h"
#include "usbd_storage_if.h"
#include "usbd_cdc_if.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
static uint8_t CurClassIndex = 0;
static USBD_ClassTypeDef const * SelectorClass[] = {
    &USBD_CDC,
    &USBD_COMP
};



/* USER CODE END PV */

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USB Device Core handle declaration. */
USBD_HandleTypeDef hUsbDeviceFS = {0};

/*
 * -- Insert your variables declaration here --
 */
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/*
 * -- Insert your external function declaration here --
 */
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/**
  * Init USB device Library, add supported class and start the library
  * @retval None
  */
void MX_USB_DEVICE_Init(void)
{
  /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
  /* USER CODE END USB_DEVICE_Init_PreTreatment */

  /* Init Device Library, add supported class and start the library. */
  if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_RegisterClass(&hUsbDeviceFS, (USBD_ClassTypeDef*)&USBD_CDC) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_COM_RegisterCommonInterface(&hUsbDeviceFS) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
  {
    Error_Handler();
  }
}

  /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */

/// @brief USBD 切换配置描述符并重新枚举
/// @return 
int8_t USBD_ChangeConfigAndReEnum()
{
    CurClassIndex = (CurClassIndex + 1) % (sizeof(SelectorClass) / sizeof(SelectorClass[0]));

    // Mp3_Stop();

    USBD_Stop(&hUsbDeviceFS);
    USB_DisConnect();
    HAL_Delay(500);
    if (USBD_RegisterClass(&hUsbDeviceFS, (USBD_ClassTypeDef*)SelectorClass[CurClassIndex]) != USBD_OK)
    {
      Error_Handler();
    }

    USBD_Start(&hUsbDeviceFS);
    
    USB_Connect();
    
    return USBD_OK;
}

void usbd_puts(const char * str)
{
    if (hUsbDeviceFS.pClassData != NULL)
    {
        USBD_CDC_SetTxBuffer(&hUsbDeviceFS, (uint8_t*)str, strlen(str));
        USBD_CDC_TransmitPacket(&hUsbDeviceFS);
    }
}


/// @brief 断开USB连接
void USB_DisConnect()
{
    USB_StopDevice(USB);

    __HAL_RCC_GPIOA_CLK_ENABLE();
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11 | GPIO_PIN_12, GPIO_PIN_RESET);

    GPIO_InitTypeDef usbgpio = {0};
    usbgpio.Mode = GPIO_MODE_OUTPUT_PP;
    usbgpio.Pin = GPIO_PIN_11 | GPIO_PIN_12;
    usbgpio.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &usbgpio);

}

/// @brief 连接USB线缆
void USB_Connect()
{
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitTypeDef usbgpio = {0};
    usbgpio.Mode = GPIO_MODE_ANALOG;
    usbgpio.Pin = GPIO_PIN_11 | GPIO_PIN_12;
    HAL_GPIO_Init(GPIOA, &usbgpio);
}
  /* USER CODE END USB_DEVICE_Init_PostTreatment */

/**
  * @}
  */

/**
  * @}
  */

